package song;

import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Aleksey
 * Date: 02.01.2008
 * Time: 18:53:38
 */
public class BarPart {
    private static Integer ID_SEQ = 0;

    private Boolean note;
    private Integer style;
    private Integer velocity;
    private TimeSignature length;
    private Integer id;

    public BarPart() {
        id = ID_SEQ++;
    }

    public Integer getId() {
        return id;
    }

    public BarPart(TimeSignature length, Boolean note, Integer style, Integer velocity) {
        this();
        this.length = length;
        this.note = note;
        this.style = style;
        this.velocity = velocity;
    }

    public static BarPart uniteParts(BarPart barPart1, BarPart barPart2) {
        BarPart barPart = new BarPart();
        barPart.note = barPart1.note ? barPart1.note : barPart2.note;
        barPart.style = barPart1.note ? barPart1.style : barPart2.style;
        barPart.velocity = barPart1.note ? barPart1.velocity : barPart2.velocity;

        barPart.length = TimeSignature.sum(barPart1.length, barPart2.length);

        return barPart;
    }

    public static BarPart uniteParts(ArrayList<BarPart> parts) {
        if (parts.size() == 1) {
            return parts.get(0);
        }
        if (parts.size() == 0) {
            return null;
        }
        BarPart barPart = uniteParts(parts.get(0), parts.get(1));
        for (int i = 2; i < parts.size(); i++) {
            barPart = uniteParts(barPart, parts.get(i));
        }
        return barPart;
    }

    protected static ArrayList<BarPart> dividePart(BarPart part) {
        ArrayList<TimeSignature> ts = part.length.divide();
        ArrayList<BarPart> parts = new ArrayList<BarPart>();
        for (int i = 0; i < ts.size(); i++) {
            BarPart barPart = new BarPart();
            barPart.note = i == 0 ? part.note : false;
            barPart.style = part.style;
            barPart.velocity = part.velocity;
            barPart.length = ts.get(i);
            parts.add(barPart);
        }
        return parts;
    }

    public TimeSignature getLength() {
        return length;
    }

    public void setLength(TimeSignature length) {
        this.length = length;
    }

    public Boolean getNote() {
        return note;
    }

    public void setNote(Boolean note) {
        this.note = note;
    }

    public Integer getStyle() {
        return style;
    }

    public void setStyle(Integer style) {
        this.style = style;
    }

    public Integer getVelocity() {
        return velocity;
    }

    public void setVelocity(Integer velocity) {
        this.velocity = velocity;
    }
}
